package com.yiwei.ydd.activity;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.text.Spanned;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.ydd.R;
import com.yiwei.ydd.adapter.VipUpRecordAdapter;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.api.model.VipComboRecordModel;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.constant.UserInfoGlobal;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.Spans;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.util.Util;
import com.yiwei.ydd.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class VipUpRecordActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.btn_login)
    TextView btnLogin;
    @BindView(R.id.img_icon_login)
    ImageView imgIconLogin;
    @BindView(R.id.img_icon)
    FrameLayout imgIcon;
    @BindView(R.id.txt_name)
    TextView txtName;
    @BindView(R.id.txt_info)
    TextView txtInfo;
    @BindView(R.id.btn_my_order)
    TextView btnMyOrder;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.layout_total)
    RelativeLayout layoutTotal;
    @BindView(R.id.img_level)
    ImageView imgLevel;
    @BindView(R.id.txt_level)
    TextView txtLevel;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;

    private Unbinder unbiner;
    private VipUpRecordAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vip_up_record);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("升级记录");
        txtRight.setText("会员订单");
        txtRight.setVisibility(View.VISIBLE);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getVipComboRecord();
            }
        });
        adapter = new VipUpRecordAdapter(this);
        list.setAdapter(adapter);
        Glide.with(this).load(UserInfoGlobal.getUserIcon()).into(imgIconLogin);
        txtName.setText(UserInfoGlobal.getUserNick());

    }

    @Override
    protected void onResume() {
        super.onResume();
        getVipComboRecord();
    }

    public void getVipComboRecord() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }


        refresh.setRefreshing(true);
        Api.api_service.getVipComboRecord().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    setData(result.datas);
                });
    }

    private void setData(VipComboRecordModel.DatasBean item) {
        if (item.user_level.equals("1")) {
            layoutTotal.setBackgroundResource(R.mipmap.img_vip_level_one_bg);
            txtLevel.setText("银卡");
            imgLevel.setImageResource(R.mipmap.img_vip_icon_yin);
            btnLogin.setBackgroundResource(R.mipmap.img_vip_grey_login);
            imgIcon.setBackgroundResource(R.mipmap.img_vip_unlogin);
            Spanned info = Spans.with(this).font("目前累计充值总额：" + item.count + "元\n", getResources().getDimensionPixelSize(R.dimen.x28), 0xffFFFFFF).style(Typeface.BOLD).
                    font("注：达到金卡会员需累计充值" + item.next_up_money + "元", getResources().getDimensionPixelSize(R.dimen.x20), 0xffFFFFFF).
                    build();
            txtInfo.setText(info);
        } else if (item.user_level.equals("2")) {
            layoutTotal.setBackgroundResource(R.mipmap.img_vip_level_two_bg);
            txtLevel.setText("金卡");
            imgLevel.setImageResource(R.mipmap.img_vip_icon_gold);
            btnLogin.setBackgroundResource(R.mipmap.img_vip_gold_login);
            imgIcon.setBackgroundResource(R.mipmap.img_vip_gold_unlogin);
            Spanned info = Spans.with(this).font("目前累计充值总额：" + item.count + "元\n", getResources().getDimensionPixelSize(R.dimen.x28), 0xffFFFFFF).style(Typeface.BOLD).
                    font("注：达到铂金会员需累计充值" + item.next_up_money + "元", getResources().getDimensionPixelSize(R.dimen.x20), 0xffFFFFFF).
                    build();
            txtInfo.setText(info);
        } else if (item.user_level.equals("3")) {
            layoutTotal.setBackgroundResource(R.mipmap.img_vip_level_three_bg);
            txtLevel.setText("铂金");
            imgLevel.setImageResource(R.mipmap.img_vip_icon_bai);
            btnLogin.setBackgroundResource(R.mipmap.img_vip_bai_login);
            btnLogin.setText("最高等级");
            imgIcon.setBackgroundResource(R.mipmap.img_vip_bai_unlogin);
            Spanned info = Spans.with(this).font("目前累计充值总额：" + item.count + "元\n", getResources().getDimensionPixelSize(R.dimen.x28), 0xffFFFFFF).style(Typeface.BOLD).
                    font("恭喜您，成为铂金会员！", getResources().getDimensionPixelSize(R.dimen.x20), 0xffFFFFFF).
                    build();
            txtInfo.setText(info);
        }

        adapter.update(item.list);
        if (adapter.getItemCount() == 0) {
            layoutEmpty.setVisibility(View.VISIBLE);
        } else {
            layoutEmpty.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.txt_right, R.id.btn_my_order})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.txt_right:
                Util.startActivity(this, VipOrderActivity.class);
                break;
            case R.id.btn_my_order:
                Util.startActivity(this, MyOrderActivity.class);
                break;
        }
    }
}
